package com.project.rfidpage.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.rfidpage.dto.RidTidMaintenanceParam;
import com.project.rfidpage.dto.RidTidMaintenanceRecordDto;
import com.project.rfidpage.dto.TidMaintenanceDto;
import com.project.rfidpage.entity.TidMaintenance;
import com.project.rfidpage.entity.TidMaintenanceRecord;
import com.project.rfidpage.mapper.TidMaintenanceMapper;
import com.project.rfidpage.mapper.TidMaintenanceRecordMapper;
import com.project.rfidpage.service.TidMaintenanceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TidMaintenanceServiceImpl implements TidMaintenanceService {

    @Resource
    private TidMaintenanceMapper tidMaintenanceMapper;

    @Resource
    private TidMaintenanceRecordMapper tidMaintenanceRecordMapper;

    @Override
    @DS("forth")
    public List<TidMaintenanceDto> getListForPage(RidTidMaintenanceParam param) {
        return tidMaintenanceMapper.selectListByUid(param);
    }

    @Override
    @DS("forth")
    public List<RidTidMaintenanceRecordDto> getRecordListForPage(RidTidMaintenanceParam param) {
        return tidMaintenanceRecordMapper.selectListByUid(param);
    }

    @Override
    @DS("forth")
    public List<TidMaintenance> getById(Long id) {
        Example example = new Example(TidMaintenance.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("task_id", id);
        return tidMaintenanceMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("forth")
    public void insert(TidMaintenance entity) {
        tidMaintenanceMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("forth")
    public void update(TidMaintenance entity, Long idBefore) {
        Example example = new Example(TidMaintenance.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("task_id", idBefore);
        tidMaintenanceMapper.updateByExampleSelective(entity, example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("forth")
    public void insertRecord(TidMaintenanceRecord entity) {
        tidMaintenanceRecordMapper.insertSelective(entity);
    }
}
